'use strict';

const Controller = require('egg').Controller;
/**
* @controller 后台管理系统
*/
class MainController extends Controller {
  /**
   * @summary 登录
   * @description 登录
   * @router post /api/admin/login
   * @request body login
   */
  async Login() {
    const username = this.ctx.request.body.username;
    const password = this.ctx.request.body.password;
    const sql = " SELECT username,type FROM admin_user WHERE username = '" + username +
      "' AND password = '" + password + "'";
    const res = await this.app.mysql.query(sql);
    if (res.length > 0) {
      // 登录成功,进行session缓存
      const token = this.ctx.app.jwt.sign({
        ...this.ctx.request.body,
      }, this.app.config.jwt.secret, {
        expiresIn: '120m', // 时间根据自己定，具体可参考jsonwebtoken插件官方说明
      });
      const type = res.type;
      // 这里根据种类做权限管理
      this.ctx.body = {
        msg: '登录成功', userinfo: {
          username,
          token,
          type,
        },
      };

    } else {
      this.ctx.body = { data: '登录失败' };
    }
  }

  /**
   * @summary 获取用户列表
   * @description 获取用户列表
   * @router get /api/admin/getUserlist
   * @response 200 useritem
   */
  async getUserlist() {
    const sql = 'SELECT admin_user.id as id,admin_user.username as name,admin_user.type as type,FROM_UNIXTIME(admin_user.createTime,"%Y-%m-%d %H:%i:%s") as createTime FROM admin_user';
    const results = await this.app.mysql.query(sql);
    console.log(results);
    const { ctx } = this;
    console.log(ctx.state.username);
    this.ctx.body = {
      data: results,
    };
    this.ctx.status = 200;
  }

  /**
   * @summary 添加用户
   * @description 添加用户
   * @router post /api/admin/AddUser
   * @request body userinfo
   */
  async addUser() {
    const username = String(this.ctx.request.body.username);
    const password = this.ctx.request.body.password;
    const confirm = this.ctx.request.body.confirm;
    const time = new Date().getTime() / 1000;
    const type = this.ctx.request.body.type;
    if (password !== confirm) {
      this.ctx.body = {
        status: 400,
        msg: '请确认两次输入同样的密码',
      };
      this.ctx.status = 400;
      return false;
    }
    const result = await this.app.mysql.insert('admin_user', { username, password, type, createTime: time });
    const insertSuccess = result.affectedRows === 1;
    if (insertSuccess) {
      this.ctx.body = {
        data: '添加成功',
      };
    } else {
      this.ctx.body = {
        msg: result.message,
      };
    }
    this.ctx.status = 200;

  }

  /**
   * @summary 删除用户
   * @description 删除用户
   * @router delete /api/admin/delUser/:id
   * @request path integer id
   */
  async delUser() {
    const id = this.ctx.params;
    const result = await this.app.mysql.delete('admin_user', {
      id,
    });
    this.ctx.body = {
      data: result,
    };
  }

  /**
   * @summary 获取文章分类
   * @class MainController
   * @router get /api/admin/artCat
   * @response 200 type
   */
  async getArtCat() {
    const sql = 'select * from type';
    const res = await this.app.mysql.query(sql);
    console.log(res);
    this.ctx.body = {
      data: res,
    };
  }
}

module.exports = MainController;
